Skip to content

[Safe code] Clean up Guid.TryFormatX - #110923

Merged
EgorBo merged 4 commits into
dotnet:mainfrom
EgorBo:guid-cleanup
Dec 27, 2024
Merged

EgorBo merged 4 commits into
dotnet:mainfrom
EgorBo:guid-cleanup

Conversation

@EgorBo

@EgorBo EgorBo commented Dec 24, 2024 •

Copy link
Copy Markdown
Member

Contributes to #94941

This PR removes unsafe code from Guid.TryFormatX + small clean up in TryParseExact.

New version is 10-15% faster.

@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 24, 2024
@EgorBo

This comment was marked as resolved.

@EgorBo

EgorBo commented Dec 24, 2024

Copy link
Copy Markdown
Member Author

@EgorBot -amd -arm -profiler

using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;

public class Benchmark
{
    static Guid guid = Guid.Parse("{D420C46B-04DC-4ABD-BF32-5132E82EE7F7}");

    [Benchmark]
    public bool FormatX()
    {
        Span<char> buffer = stackalloc char[100];
        bool result = guid.TryFormat(buffer, out int charsWritten, "X");
        Consume(buffer.Slice(0, charsWritten));
        return result;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(Span<char> _){}
}

@jkotas jkotas added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 24, 2024
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@EgorBo

This comment was marked as resolved.

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New version is 10-15% faster.

I'm guessing that's from HexsToCharsHexOutput not being inlined before?

@EgorBo

EgorBo commented Dec 26, 2024

Copy link
Copy Markdown
Member Author

I'm guessing that's from HexsToCharsHexOutput not being inlined before?

Yep, but the goal was to remove the unsafe code & maintain same perf as before 🙂

success = TryParseExactX(input, ref parseResult);
break;
}
bool success = (char)(format[0] | 0x20) switch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the cast to char actually necessary here? It should work without it and that would make smaller IL and avoid an extra zero extension that the JIT has to otherwise remove here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannergooding hm. I've removed the cast but the codegen hasn't changed. But I guess it is still makes sense to remove it as useless

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd guess the JIT is recognizing it as unnecessary and removing it already; just something small that caught my eye as likely unneeded

@EgorBo

EgorBo commented Dec 27, 2024

Copy link
Copy Markdown
Member Author

/ba-g "browser-wasm linux Release LibraryTests is stuck"

@EgorBo
EgorBo merged commit 2eb59bf into dotnet:main Dec 27, 2024
@EgorBo
EgorBo deleted the guid-cleanup branch December 27, 2024 20:30
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants